﻿using MqttLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace remote_config_tools
{
    public class fileOp
    {
        private Form1 fm;
        private static Mutex mutex ;
        public static AutoResetEvent myResetEvent = new AutoResetEvent(false);
        public static long cur_recv_pack_num;
        public static int has_come = 0;
        public fileOp(Form1 fm)
        {
            this.fm = fm;
            mutex = new Mutex();
            myResetEvent = new AutoResetEvent(false);
        }
        public void fileDownload(object obj)
        {   
            //获取互斥量
            mutex.WaitOne();
            //获取文件名称
            string filename = obj as string;
            //通过文件名称获取获取文件信息对象
            FileInfo file = new FileInfo(filename);
            //获取文件长度
            long file_len = file.Length;
            //读文件缓冲区
            byte[] send;
            //重试次数
            int try_times = 0;
            //发送包数计数
            long cur_send_pack_num =0;
            //获取要发送的总包数
            long cur_pack_num = file_len/64;
            if (file_len % 64 > 0) cur_pack_num += 1;
            //使用“打开”对话框中择的文件名实例化FileStream对象
            FileStream myStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            //使用FileStream对象实例化BinaryReader二进制写入流对象
            BinaryReader myReader = new BinaryReader(myStream);
            fm.ctrlobj.UpdataProgessMin(fm.getParaprogress(), 0);
            fm.ctrlobj.UpdataProgessMax(fm.getParaprogress(), (int)cur_pack_num);
            fm.ctrlobj.UpdataButtonEnable(fm.getButton(),false);
            fm.ctrlobj.UpLabelText(fm.getUpgradeStateLabel(), "升级中.........");
            do
            {
                //以二进制方式读取文件中的内容
                send = myReader.ReadBytes(64);
                if (send.Length <= 0) break;
                try_times = 0;
                has_come = 0;
                myResetEvent.Reset();
                do
                {
                    //发送数据包
                    fm.mtlib.mqttPublishSomething("/fw/alphair2/" + fm.input_mac + "/down", dt_protocol.wrapper_upgrade_data(send, 01, cur_pack_num, cur_send_pack_num), QoS.BestEfforts, false);
                    //等待超时了
                    if (true == myResetEvent.WaitOne(5000)) break;
                    if (++try_times > 4) break;

                } while (true);

                if (try_times > 4) break;
                //数据漏包了
                if (cur_send_pack_num != cur_recv_pack_num) break;
                //发送成功计数
                cur_send_pack_num++;
                fm.ctrlobj.UpLabelText(fm.getUpgradePackNumLabel(), "数据包:"+cur_send_pack_num.ToString()+"/"+ cur_pack_num.ToString());
                //更新进度条显示
                fm.ctrlobj.UpdataProgessValue(fm.getParaprogress(), (int)cur_send_pack_num);
                string labeltext = (cur_send_pack_num * 100 / cur_pack_num).ToString() + "%";
                fm.ctrlobj.UpLabelText(fm.getUpgradeLabel(), labeltext);
                if (cur_send_pack_num == cur_pack_num) break;

            } while (true);
            //升级显示
            if (cur_send_pack_num == cur_pack_num )
            {
                fm.ctrlobj.UpLabelText(fm.getUpgradeStateLabel(), "升级成功！！！");
            }
            else
            {
                fm.ctrlobj.UpLabelText(fm.getUpgradeStateLabel(), "升级失败！！！");
            }
            fm.ctrlobj.UpdataButtonEnable(fm.getButton(), true);
            //关闭当前二进制读取流
            myReader.Close();
            //关闭当前文件流
            myStream.Close();
            //释放互斥量
            mutex.ReleaseMutex();
        }
    }
}
